#include <cassert>
#include "sec_sed.h"


SecSed::CloudA::CloudA(const PrivateKey& pri):
	m_a1(pri), m_a2(pri){
	// ...
}

void SecSed::CloudA::dec_reenc(const std::vector<NTL::ZZ>& ncva, const std::vector<NTL::ZZ>& ncvb, 
	std::vector<NTL::ZZ>& cvmul){
	
	assert(ncva.size() == 2 && ncvb.size() == 2);
	
	cvmul.clear();
	
	NTL::ZZ cmul;
	
	m_a1.dec_reenc(ncva[0], ncvb[0], cmul);
	cvmul.push_back(cmul);
	
	m_a2.dec_reenc(ncva[1], ncvb[1], cmul);
	cvmul.push_back(cmul);
}

SecSed::CloudB::CloudB(const PublicKey& pub):
	m_b1(pub), m_b2(pub){
	// ...
}


void SecSed::CloudB::gen_two_ciphers(const std::vector<NTL::ZZ>& cva, const std::vector<NTL::ZZ>& cvb,
	std::vector<NTL::ZZ>& ncva, std::vector<NTL::ZZ>& ncvb){
	
	assert(cva.size() == 2 && cvb.size() == 2);
	
	ncva.clear();
	ncvb.clear();
	
	NTL::ZZ ca01, cb01, nca, ncb;
	
	m_b1.HE_Mul_Const(cva[0], m_b1.GetN() -1, ca01);
	m_b1.HE_Add_Cipher(ca01, cvb[0], ca01);
	
	m_b1.gen_two_ciphers(ca01, ca01, nca, ncb);
	ncva.push_back(nca);
	ncvb.push_back(ncb);
	
	m_b1.HE_Mul_Const(cva[1], m_b1.GetN() -1, cb01);
	m_b1.HE_Add_Cipher(cb01, cvb[1], cb01);
	
	m_b2.gen_two_ciphers(cb01, cb01, nca, ncb);
	ncva.push_back(nca);
	ncvb.push_back(ncb);
}

void SecSed::CloudB::gen_mul(const std::vector<NTL::ZZ>& cvmul, NTL::ZZ& csed){
	assert(cvmul.size() == 2);
	
	NTL::ZZ mul0, mul1;
	
	m_b1.gen_mul(cvmul[0], mul0);
	m_b2.gen_mul(cvmul[1], mul1);
	
	m_b1.HE_Add_Cipher(mul0, mul1, csed);
}
